package com.ben.multiple.poi.rest;

import com.ben.multiple.poi.BeanMapper;
import com.ben.multiple.poi.PoiUtils;
import com.ben.multiple.poi.model.Student;
import com.ben.multiple.poi.model.StudentDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ben
 * @since 2019-06-02
 */
@RestController
@RequestMapping("/student")
@Slf4j
public class StudentResource {

    private static final List<Student> STUDENT_LIST = new ArrayList<>();

    static {
        STUDENT_LIST.add(new Student(1, "刘邦", 18, LocalDate.now().plusYears(-2)));
        STUDENT_LIST.add(new Student(10, "刘婵", 180, LocalDate.now()));
        STUDENT_LIST.add(new Student(100, "刘杰", 12, LocalDate.now().plusYears(2)));
    }

    @GetMapping("/list")
    public ResponseEntity<List<Student>> listStudent() {
        return ResponseEntity.ok(STUDENT_LIST);
    }

    @PostMapping("/import")
    public ResponseEntity<Void> importStudent(@RequestParam(value = "file") MultipartFile file) {
        Workbook workbook;
        try {
            workbook = WorkbookFactory.create(file.getInputStream());
        } catch (Exception e) {
            log.error("文件格式异常", e);
            throw new RuntimeException("文件格式异常");
        }
        if (workbook == null || workbook.getNumberOfSheets() == 0) {
            throw new RuntimeException("文件格式异常");
        }
        Sheet sheet = workbook.getSheetAt(0);
        List<Map<String, String>> data = PoiUtils.getDataFromSheet(sheet, 1, StudentDTO.IMPORT_HEADER);
        List<StudentDTO> dtoList = data.stream().map(obj ->
                BeanMapper.map(obj, Map.class, StudentDTO.class)).collect(Collectors.toList());
        log.info("导入的数据：{}", dtoList);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/export")
    public void exportStudent(HttpServletResponse response) {
        List<List<String>> data = getDataFromWorkbook(STUDENT_LIST);
        Workbook workbook = PoiUtils.generateWorkbook(data, StudentDTO.EXPORT_HEADER);
        PoiUtils.responseWorkbook(response, workbook);
    }

    private List<List<String>> getDataFromWorkbook(List<Student> list) {
        List<List<String>> data = new ArrayList<>();
        for (Student obj : list) {
            List<String> rowData = new ArrayList<>();
            rowData.add(obj.getId().toString());
            rowData.add(obj.getName());
            rowData.add(obj.getAge().toString());
            rowData.add(obj.getBirthday().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            data.add(rowData);
        }
        return data;
    }
}
